import * as proxy from '../proxy';
import ServiceBase from '../base';

const GET_MESSAGES = '/box/messages';
const GET_ACCOUNTS = 'accounts';
class MessagesService extends ServiceBase {
    // GET /box/messages 获取私信
    getPrivateMessages = (data) => {
        let promise = super.get({
            url: GET_MESSAGES,
            params: data,
        });
        return proxy.httpResultProxy(promise);
    };
    // POST /box/messages 创建一条私信
    postPrivateMessage = (data) => {
        let promise = super.post({
            url: GET_MESSAGES,
            data: data,
        });
        return proxy.httpResultProxy(promise);
    };
    //PUT  /box/messages     更新私信状态;已读,未读
    putPrivateMessage = (data) => {
        let promise = super.put({
            url: GET_MESSAGES,
            data,
        });
        return proxy.httpResultProxy(promise);
    };
    //GET  /box/messages/notices   获取通知,包括批准,任务,订阅,广播等产生的消息
    getUserMessages = (data) => {
        let promise = super.get({
            url: GET_MESSAGES + '/notices',
            params: data,
        });
        return proxy.httpResultProxy(promise);
    };
    getUserMessagesByTypes = (data) => {
        const types = data && data.types && data.types.length ? `types=${data.types.join('&types=')}` : '';
        const subtypes = data && data.subtypes && data.subtypes.length ? `subtypes=${data.subtypes.join('&subtypes=')}` : '';
        let promise = super.get({
            url: `${GET_MESSAGES}/notices?${types}&${subtypes}`,
        });
        return proxy.httpResultProxy(promise);
    };
    //PUT /box/messages/state 更新'我的消息'的状态
    putMessageState = (data) => {
        let promise = super.put({
            url: GET_MESSAGES + '/state',
            data,
        });
        return proxy.httpResultProxy(promise);
    };
    // GET /box/messages/count/unread 获取未读消息的数量
    getUnreadMessages = () => {
        let promise = super.get({
            url: GET_MESSAGES + '/count/unread?types=0&types=1&types=2&types=3&types=4&types=5',
        });
        return proxy.httpResultProxy(promise);
    };
    getUnreadMessagesByType = (data) => {
        const types = data && data.types && data.types.length ? `types=${data.types.join('&types=')}` : '';
        const subtypes = data && data.subtypes && data.subtypes.length ? `subtypes=${data.subtypes.join('&subtypes=')}` : '';
        let promise = super.get({
            url: `${GET_MESSAGES}/count/unread?${types}&${subtypes}`,
        });
        return proxy.httpResultProxy(promise);
    };
    getAccount = () => {
        let promise = super.get({
            url: GET_ACCOUNTS,
        });
        return proxy.httpResultProxy(promise);
    };
    getProject = (data) => {
        let promise = super.get({
            url: GET_ACCOUNTS + `/${data}/projects`,
        });
        return proxy.httpResultProxy(promise);
    };
}

export {MessagesService};
